import React, { useEffect, useState } from 'react';
import { Form } from 'antd';
import { AxCreateGridThreeForm } from '@/components/AxForm/AxCreateForm';
import { AxDatePicker, AxInput, AxInputNumber, AxSelectEnum, AxSelectSearch, AxSelectTree } from '@/components/AxForm/AxFormItem';
import { axurl } from '@/shared/axurl';
import { axhttp } from '@/shared/axhttp';
import { axhelper } from '@/shared/axhelper';
import { AxUploadPicture } from '@/components/AxForm/AxFormItem/AxUpload';

const url = axurl.store.goods.data;
const { getDataX } = axhttp;
const Create: React.FC<any> = props => {
    const { form, ...rest } = props;
    const [categorys, setCategorys] = useState<any>([]);
    const [unit, setUnit] = useState<any>([]);

    useEffect(() => {
        getDataX(url.readCategory).then((res: any) => {
            setCategorys(axhelper.transToStandardData(res.list, 'id', 'text'));
        });
    }, []);

    const handleReadUnits = (character: any) => {
        getDataX(url.readUnits, { character }).then((res: any) => {
            setUnit(res.list.map((m: any) => ({
                text: m,
                value: m,
            })));
        });
    }

    return (
        <AxCreateGridThreeForm
            form={form}
            title='添加商品'
            {...rest}
            items={[
                <AxInput
                    form={form}
                    label="产品名称"
                    field="name"
                    required={false}
                />,
                <AxInput
                    form={form}
                    label="英文名称"
                    field="eName"
                    required={false}
                />,
                <AxInput
                    form={form}
                    label="产品别名"
                    field="alias"
                    required={false}
                />,
                <AxInput
                    form={form}
                    label="产品规格"
                    field="productSpec"
                    required={false}
                />,
                <AxInput
                    form={form}
                    label="生产厂家"
                    field="factory"
                    required={false}
                />,
                <AxInput
                    form={form}
                    label="品牌"
                    field="brand"
                    required={false}
                />,
                <AxInput
                    form={form}
                    label="分子式"
                    field="formula"
                    required={false}
                />,
                <AxInput
                    form={form}
                    label="技术参数"
                    field="techParameter"
                    required={false}
                />,
                <AxInput
                    form={form}
                    label="分子量"
                    field="molecular"
                    required={false}
                />,
                <AxInput
                    form={form}
                    label="库存"
                    field="stock"
                    required={false}
                />,
                <AxInput
                    form={form}
                    label="CAS编码"
                    field="casCode"
                    required={false}
                />,
                <AxSelectTree
                    form={form}
                    label="产品分类"
                    field="categoryId"
                    required={false}
                    data={categorys}
                />,
                <AxSelectEnum
                    form={form}
                    label="产品性状"
                    field="character"
                    required={false}
                    enumName='character'
                    onChange={handleReadUnits}
                />,
                <AxInput
                    form={form}
                    label="纯度"
                    field="purity"
                    required={false}
                />,
                <AxInputNumber
                    form={form}
                    label="单价"
                    field="price"
                    required={false}
                />,
                <AxSelectSearch
                    form={form}
                    label="单位"
                    field="unit"
                    data={unit}
                    required={false}
                />,
                <AxInput
                    form={form}
                    label="包装规格"
                    field="packingSpec"
                    required={false}
                />,
                <AxInput
                    form={form}
                    label="单件容量"
                    field="oneCapacity"
                    required={false}
                />,
                <AxInput
                    form={form}
                    label="单件包装单位"
                    field="onePackingUnit"
                    required={false}
                />,
                <AxInput
                    form={form}
                    label="包装单位"
                    field="packingUnit"
                    required={false}
                />,
                <AxDatePicker
                    form={form}
                    label="生产日期"
                    field="productDate"
                    format='YYYY-MM-DD'
                    required={false}
                />,
                <AxInputNumber
                    form={form}
                    label="有效期天数"
                    field="validityDays"
                    required={false}
                />,
                <AxUploadPicture
                    form={form}
                    label="文件"
                    field="attachmentIds"
                    uploadUrl={axurl.comm.uploadFile}
                    wholerow="three"
                />
            ]}
        />
    );
};

export default Form.create<any>()(Create);
